package com.hitqz.robot.driver.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class TrackCacheUtil {

    public static final String KEY_STOP_STATUS = "stopStatus";
    public static final String KEY_ULTRASONIC_DISTANCE = "ultrasonicDistance";
    public static final String KEY_OBSTACLE_AVOIDANCE_ENABLE = "obstacleAvoidanceEnable";
    public static final String KEY_MILEAGE_CALIBRATION_FACTOR = "mileageCalibrationFactor";
    public static final String KEY_RFID_READER_POWER = "rfidReaderPower";
    public static final String KEY_POWER_INFO = "powerInfo";
    public static final String KEY_CHARGE_STATUS = "chargeStatus";
    public static final String KEY_AIR_QUALITY = "airQuality";
    public static final String KEY_LOCATE_STATUS = "locateStatus";
    public static final String KEY_CALCULATE_LOCATION = "calculateLocation";
    public static final String KEY_CALIBRATION_POINT_INFO = "calibrationPointInfo";
    public static final String KEY_ULTRASONIC_REALTIME_DATA = "ultrasonicRealtimeData";


    private static final Cache<String, Boolean> speedFlagCache = CacheBuilder.newBuilder()
            .maximumSize(1)
            .expireAfterWrite(800, TimeUnit.MILLISECONDS) // 缓存过期时间
            .build();

    private static final Cache<String, Object> cache = CacheBuilder.newBuilder()
            .maximumSize(50)// fixme 缓存最大容量,按key实际种类去扩容
            .expireAfterWrite(5, TimeUnit.SECONDS) // 缓存过期时间
            .build();

    public static void setSpeedFlag(){
        speedFlagCache.put("speed", true);
    }

    public static boolean getSpeedFlag(){
        return Optional.ofNullable( speedFlagCache.getIfPresent("speed")).orElse(false);
    }

    // 使用命令 号做 key
    public static void put(String key, Object value) {
        cache.put(key, value);
    }

    public static Object get(String key) {
        return cache.getIfPresent(key);
    }

}
